<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 */
namespace app\admin\model;

use common\Auth;
use common\Checkrole;
use app\admin\model\Model;
use Mscore\Core\Lang;

class Login extends Model{

    static private $module; //设置模块名称
    static private $roleset; //设置角色功能表
    static private $user; //设置角色功能表
    private $token;  //登陆token
    
    /**
     *  用户登陆
     * @param $token
     */
    public function userAuth($username='',$passwd='',$ip=''){
        $info = (new Auth)->userAuth($username,$passwd,$ip);
        if(isset($info['data'])){
            setCert($info['data']);//设置凭证
        }
        return $info;
    }
    /**
     *  用户检测
     * @param $token
     */
    public function isAuth($token=null){
        $authset=config('session');
        switch (true) {
            case $authset["usesession"]==true: //使用session
                session_start();
                $this->token=isset($_SESSION["token"])?$_SESSION["token"]:'';
                break;
            case $authset["usecookie"]==true: //使用cookie
                $this->token=isset($_COOKIE["token"])?$_COOKIE["token"]:'';
                break;
            default:
                $this->token=$token; //传入token
                break;
        }
        unset($authset);
        $user = (new Auth)->isAuth($this->token);
        switch ($user) {
            case $user['code']==200:
                unset($user['data']['token']);
                self::$user=['roletype'=>$user['data']['info']['roletype'],'grade'=>$user['data']['info']['grade']];
                $data['info']=$user['data']['info'];
                $data['checkrole']=self::getRolePerAll();
                return $data;
                break;
            //数据不存在退出
            default:
                $language = isset($_COOKIE['language'])?$_COOKIE['language']:config('langs.default');
                Lang::init($language);
                return Lang::get('Tokenexp');
                break;
        }
    }
    
    /**
     * 退出登录
     * @param array $module 
     * @param array $user 
    */
    public function logout($token=null){
        $authset=config('session');
        switch (true) {
            case $authset["usesession"]==true: //使用session
                session_start();
                $this->token=$_SESSION["token"];
                break;
            case $authset["usecookie"]==true: //使用cookie
                $this->token=$_COOKIE["token"];
                break;
            default:
                $this->token=$token; //传入token
                break;
        }
        return (new Auth)->logout($this->token);
    }

    /**
     * 菜单控制
     * @param array $getRules 
     * @param array $user 
    */
    public static function getRules(){
        //模块菜单控制
        $data=Checkrole::getRules(self::$user);
        return  $data;
    }

    /**
     * 权限检查
     * @param array $checkRolePer 
     * @param int $module_id  string $key
    */
    public static function checkRolePer($module_id,$key){
        //模块权限控制
        $data=Checkrole::checkRolePer($module_id,$key,self::$user);
        return  $data;
    }
    /**
     * 权限检查
     * @param array $getRolePerAll 
     * @param array $user 
    */
    public static function getRolePerAll(){
        //模块权限控制
         $data=Checkrole::getRolePerAll(self::$user);
         return  $data;
    }        

}